﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using AlexandriaPressPublisherBusinessLayer;
using AlexandriaPressUserBusinessLayer;
using MailSender;
using AlexandriaPressUserBusinessLayer;

namespace AlexandriaPressWebsite.MasterPages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (HttpContext.Current.User.Identity.Name != "")
            {
                UserBusinsessLayer business = new UserBusinsessLayer();
                string _UserType = business.getUserType(HttpContext.Current.User.Identity.Name);
                if (_UserType == "Type_ProofReader")
                {

                }
                else
                {
                    Response.Redirect("~/MasterPages/ErrorPage.aspx");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
            content.Visible = false;
            int draftID = 0;
            bool sendEmail = false;
            try
            {
                NotificationLabel.Text = ProofreaderProcessManager.deadlineNotification(Convert.ToInt32(Session["userID"]), ref draftID, ref sendEmail);
            }
            catch (Exception err)
            {
                throw err;
            }

            if (draftID > 0)
            {
                Session.Add("draftID", draftID);
                FileUploadConfBtn.Visible = true;
            }
            else
            {
                UnassignedDraftsBtn.Visible = true;
            }

            if (sendEmail)
            {
                string bodyOfEmail = HttpContext.Current.User.Identity.Name + " is late submitting Draft ID#" + Convert.ToString("transID");
                UserBusinsessLayer user = new UserBusinsessLayer();
                EmailNotification.AdminMail(user.getUser(HttpContext.Current.User.Identity.Name).Email, "admin@alexandria.com", "bcc", "cc", "Overdue Draft!", bodyOfEmail);
            }
        }

        protected void uploadSubmitBtn_Click(object sender, EventArgs e)
        { 
                if (fileUploader.HasFile)
                {
                    if (fileUploader.FileName.Trim().Length > 100)
                    {
                        messageLabel.Text = "Please use a shorter file name.";
                    }
                    else
                    {
                        try
                        {
                            messageLabel.Text = ProofreaderProcessManager.reviewedDraftUpload(fileUploader.FileName, Server.MapPath("~\\Files\\" + fileUploader.FileName), fileUploader.PostedFile, Convert.ToInt32(Session["userID"]), Convert.ToInt32(Session["draftID"]));
                            if (messageLabel.Text == "File uploaded!")
                            {
                                NotificationLabel.Text = "You do not have any pending draft to review.";
                                uploadLabel.Visible = false;
                                fileUploader.Visible = false;
                                uploadSubmitBtn.Visible = false;
                                UnassignedDraftsBtn.Visible = true;
                                headerLabel.Visible = false;
                                recommendationLabel.Visible = false;
                                FindingsDropDown.Visible = false;
                                messageLabel.Text += "<br/><br/>";
                                FileUploadConfBtn.Visible = false;
                            }
                            Session.Remove("draftID");
                        }
                        catch (Exception ex)
                        {
                            messageLabel.Text = "File could not be uploaded.<br/><br/>";
                        }
                    }
                }         
        }

        protected void FileUploadConfBtn_Click(object sender, EventArgs e)
        {
            FindingsLabel.Visible = true;
            FindingsDropDown.Visible = true;
            commentText.Visible = true;
            FindingsSubmitBtn.Visible = true;
            recommendationLabel.Visible = true;
            FileUploadConfBtn.Visible = false;
            headerLabel.Visible = true;
        }

        protected void FindingsSubmitBtn_Click(object sender, EventArgs e)
        {
            FindingsSubmitBtn.Visible = false;
            UserBusinsessLayer user = new UserBusinsessLayer();
            int i = ProofreaderProcessManager.submitProofreaderFindings(user.getUser(HttpContext.Current.User.Identity.Name).UserID, Convert.ToInt32(Session["draftID"]), FindingsDropDown.SelectedItem.Value, commentText.Value.Trim().Replace("'","''"));
            if (i > 0)
            {
                UnassignedDraftsBtn.Visible = true;
                FindingsLabel.Visible = false;
                FindingsDropDown.Visible = false;
                commentText.Visible = false;
                FindingsSubmitBtn.Visible = false;
                recommendationLabel.Visible = false;
                NotificationLabel.Text = "You do not have any pending draft to review.";
                headerLabel.Visible = false;
                FileUploadConfBtn.Visible = false;
            }
            else
            {
                NotificationLabel.Text = "An error occurred.";
            }
        }

        protected void UnassignedDraftsBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/MasterPages/ProofReaderPages/AllDraftsForReview.aspx");
        }

        protected void FindingsDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FindingsDropDown.SelectedItem.Value == "Approve")
            {
                ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
                content.Visible = true;
                FindingsSubmitBtn.Visible = false;
                FindingsLabel.Visible = false;
                commentText.Visible = false;
                FindingsSubmitBtn.Visible = false;
                headerLabel.Visible = false;
            }
            else
            {
                ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
                content.Visible = false;
                FindingsSubmitBtn.Visible = true;
                FindingsLabel.Visible = true;
                commentText.Visible = true;
                FindingsSubmitBtn.Visible = true;
                headerLabel.Visible = true;          
            }
            FileUploadConfBtn.Visible = false;
        }

    }
}
